/**
 * Created by apple on 2017/6/25.
 */
import React,{Component} from 'react';
import {
  View,Text,
  StyleSheet,
  AsyncStorage,
  TextInput,
} from "react-native";
import Toast,{DURATION} from 'react-native-easy-toast'
import NavigationBar from "./common/NaviagtionBar";
const KEY1='test';
export default class AsyncStorageTest extends Component{
  constructor(props){
    super(props);
    this.state= {
      word:''
    }
  }
  onSave(){
    console.log(this.text)
    data= this.text===undefined?'没有输入任何文字':this.text
    AsyncStorage.setItem(KEY1,data,(err)=>{
      if(!err){
        this.toast.show("保存成功"+data,DURATION.LENGTH_LONG)
      }else{
        this.toast.show("保存失败",DURATION.LENGTH_LONG)
      }
    })
  }
  onFetch(){
    AsyncStorage.getItem(KEY1,(err,result)=>{
      if(!err){
        if(result!==''&&result!==null){

        this.toast.show('提取成功:'+result)
        }
      }else{
        this.toast.show("提出失败")
      }
    })
  }
  onRemove(){
    AsyncStorage.removeItem('KEY1',err =>{
      if(!err){
        this.toast.show('移除成功')
      }else{
        this.toast.show("移除失败")
      }
    })
  }
  render(){
    return (<View style={styles.container}>
        <NavigationBar
          title="AsyncStorage"
          style={{
            backgroundColor:'red'
          }}
          statusBar={{
            backgroundColor:"red"
          }}
        />
        <TextInput style={{borderWidth:1,height:40}}
          onChangeText={(text) =>this.text=text }
        />
       <View style={{flexDirection:'row'}}>
         <Text style={styles.tips} onPress={() =>this.onSave()}>
           保存
         </Text><Text style={styles.tips} onPress={() =>this.onRemove()}>
         移除
       </Text><Text style={styles.tips} onPress={() =>this.onFetch()}>
         取出
       </Text>

       </View>
        <Toast ref={toast => this.toast=toast}/>
      </View>
    )
  }


}
const styles = StyleSheet.create({
  container:{
    flex:1,
    backgroundColor:"gray",
  },
  text:{
    fontSize:20
  },
  tips:{
    margin:5,
    fontSize:20,
  },
})
